package controllers;
import static play.data.Form.form;
import java.util.List;
import com.avaje.ebean.Page;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import controllers.TableRequest.StringPair;
import models.Customer;
import play.data.Form;
import play.libs.Json;
import play.mvc.*;
import models.Account;
import logger.LogSchedular;
public class Customers extends Controller{
    public static Result search() {
      Form<TableRequest> reqform= form(TableRequest.class).bindFromRequest();
      TableRequest req = null;
      if (reqform.hasErrors()) {
          return internalServerError(InternalErrorMsg.paramError(reqform.errorsAsJson().toString()));
      } else {
          req = reqform.get();
      }
      int total = 0;
      List<Customer> all = null;
      try {
          int pageIndex = req.iDisplayStart / req.iDisplayLength;
          StringPair sort = req.getSortPair();
          Page<Customer> page = Customer.page(pageIndex, req.iDisplayLength, sort.str1, sort.str2, req.getSearchColums(), req.getSearchStrings());
          all = page.getList();
          total = page.getTotalRowCount();
      } catch (Exception e) {
          return internalServerError(InternalErrorMsg.getError(e.getMessage()));
      }
      TableResponse<Customer> resp = new TableResponse<Customer>();
      resp.iTotalDisplayRecords = total;
      resp.iTotalRecords = total;
      resp.aaData = all;
      resp.sEcho = req.sEcho;
      return ok(Json.toJson(resp));
  }
  public static Result add() {
      Form<Customer> aForm = form(Customer.class).bindFromRequest();
      Customer aModel = null;
      if (aForm.hasErrors()) {
          return internalServerError(InternalErrorMsg.paramError(aForm.errorsAsJson().toString()));
      } 
      try {
          aModel = aForm.get();
          aModel.save();
          LogSchedular.logAdd(Account.findByName(session("name")), "customer", aModel.getId());
        } catch (Exception e) {
            return internalServerError(InternalErrorMsg.getError(e.getMessage()));
        }
        return ok(Json.toJson(aModel));
    }
    public static Result delete() {
        Form<DeleteRequest> f = form(DeleteRequest.class).bindFromRequest();
        if (f.hasErrors()) {
            return internalServerError(InternalErrorMsg.paramError(f.errorsAsJson().toString()));
        } 
        try {
            Customer.find.byId(f.get().id).delete();
            LogSchedular.logDelete(Account.findByName(session("name")), "customer", f.get().id);
        } catch (Exception e) {
            return internalServerError(InternalErrorMsg.getError(e.getMessage()));
        }
        return ok("ok");
    }
    public static Result update() {
        Form<UpdateRequest> f = form(UpdateRequest.class).bindFromRequest();
        if (f.hasErrors()) {
            return internalServerError(InternalErrorMsg.getError(f.errorsAsJson().toString()));
        } 
        try {
            UpdateRequest r = f.get();
            Customer aModel = Customer.find.byId(r.id);
            String[] fields = r.columnName.split("\\.");
            if (fields.length == 1) {
                BeanRefUtil.updateField(aModel, r.columnName, r.value);
            } else if (fields.length == 2) {
            }
            aModel.update();
            LogSchedular.logModify(Account.findByName(session("name")), "customer", r.id, r.columnName);
            return ok(r.value);
        } catch (Exception e) {
            return internalServerError(InternalErrorMsg.getError(e.getMessage()));
        }
    }
    public static Result select() {
        List<Customer> all = Customer.find.all();
        ObjectNode node = Json.newObject();
        for(Customer v: all) {
            node.put(v.id.toString(), v.getId()); 
        }
        return ok(node.toString());
    }
}
